package ru.miit.elearning.vtutor.check.object;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import ru.miit.elearning.vtutor.check.CheckObjectResult;
import ru.miit.elearning.vtutor.data.CheckObject;
import ru.miit.elearning.vtutor.ftp.FTPClientDecorator;
import ru.miit.elearning.vtutor.wsclient.VlStudent;

/**
 *
 * @author Cyclone
 */
public abstract class ZOSAbstractCheckObject extends AbstractCheckObject {

    public ZOSAbstractCheckObject(CheckObject co) {
        super(co);
    }

    @Override
    public CheckObjectResult check(VlStudent student, FTPClientDecorator ftp) {
        CheckObjectResult cor = new CheckObjectResult();
        JSONObject jsonCheck = null;

        try {
            jsonCheck = new JSONObject();

            path = path.replaceAll("<userid>", student.getZOSHomeFolder());
            path = path.toUpperCase();
            jsonCheck.put("path", ("//" + student.getZOSHomeFolder().toUpperCase() + "." + path));
            jsonCheck.put("type", type);

            boolean isObjectListed = isObjectListed(ftp);

            if (!isObjectListed) {
                return getNotFoundCheckDetails(cor, jsonCheck);
            }

            // need to download object?
            if (content != null && content.length > 0) {
                // object found, need to download

                String objectContent = getTextContent(ftp);
                if (objectContent == null) {
                    return getNotFoundCheckDetails(cor, jsonCheck);
                }
                // file found, checking content

                boolean objectChecked = true;
                JSONArray jsonCombs = new JSONArray();
                for (String comb : content) {
                    comb = comb.replaceAll("<userid>", student.getZOSHomeFolder());
                    comb = comb.toUpperCase();

                    JSONObject jsonComb = new JSONObject();
                    jsonComb.put("comb", comb);
                    // TODO Removed REGEXP here
                    if (comb.equalsIgnoreCase("<notnull>")) {
                        if (objectContent.length() > 0) {
                            jsonComb.put("check", AbstractCheckObject.COMB_FOUND);
                        } else {
                            objectChecked = false;
                            jsonComb.put("check", AbstractCheckObject.COMB_NOT_FOUND);
                        }
                    } else {
                        // if(!contentString.matches(".*" + content[i] + ".*"))
                        if (objectContent.contains(comb)) {
                            jsonComb.put("check", AbstractCheckObject.COMB_FOUND);
                        } else {
                            jsonComb.put("check", AbstractCheckObject.COMB_NOT_FOUND);
                            objectChecked = false;
                        }
                    }
                    jsonCombs.put(jsonComb);
                }

                jsonCheck.put("combs", jsonCombs);
                jsonCheck.put("status", AbstractCheckObject.STATUS_FOUND_AND_CHECKING);
                jsonCheck.put("result", objectChecked ? AbstractCheckObject.RESULT_CHECK_OK : AbstractCheckObject.RESULT_CHECK_FAILED);
                cor.setChecked(objectChecked);
                cor.setDetails(jsonCheck);
                return cor;
            } else {
                // file found, no need to check
                return getFoundAndCheckedCheckDetails(cor, jsonCheck);
            }

        } catch (Exception e) {
            Logger.getLogger(ZOSAbstractCheckObject.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
            try {
                jsonCheck.put("status", AbstractCheckObject.STATUS_EXCEPTION);
                jsonCheck.put("result", AbstractCheckObject.RESULT_CHECK_FAILED);
            } catch (JSONException ex) {
                Logger.getLogger(ZOSAbstractCheckObject.class.getName()).log(Level.SEVERE, null, ex);
            }

            cor.setChecked(false);
            cor.setDetails(jsonCheck);
            return cor;

        }
    }

    @Override
    public abstract String getTextContent(FTPClientDecorator ftp);

    protected abstract boolean isObjectListed(FTPClientDecorator ftp);
}
